package com.example.practice;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


/**
 * A simple {@link Fragment} subclass.
 */
public class AerFragment extends Fragment {

    private View view;
    private BarChart aerChart;
    private TimerTask timerTask;
    private Timer timer;
    private List<String> list;
    private SQLiteDatabase db;
    public AerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onPause() {
        super.onPause();
        //timer.cancel();
        timer=null;
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_aer, container, false);
        initView();
        timer=new Timer();
        timerTask=new TimerTask() {
            @Override
            public void run() {
                initData();
            }
        };
        timer.schedule(timerTask,0,3000);
        return view;
    }

    private void initData() {
        int s = 0;
        int i =1;
        list = new ArrayList<>();
        ArrayList<BarEntry> values = new ArrayList<>();
        Cursor info1 = db.query("weather", null, null,null, null, null, null);
        if(info1!= null){
            while(info1.moveToNext()) {
                if (s<info1.getInt(info1.getColumnIndex("weather5"))) {
                    s=info1.getInt(info1.getColumnIndex("weather5"));
                }
                list.add(info1.getString(info1.getColumnIndex("weather7")));
                values.add(new BarEntry(i,info1.getInt(info1.getColumnIndex("weather5"))));
                i++;
            }
        }
        BarDataSet set = new BarDataSet(values, "过去1分钟空气质量最大值："+s);
        set.setValueTextSize(10f);
        XAxis xAxis = aerChart.getXAxis();
        xAxis.setLabelCount(20);
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        YAxis yAxisLeft = aerChart.getAxisLeft();
        yAxisLeft.setDrawAxisLine(false);
        yAxisLeft.setAxisMinimum(0f);
        YAxis yAxisRight = aerChart.getAxisRight();
        yAxisRight.setEnabled(false);
        Legend legend=aerChart.getLegend();
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP );
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); //图例 水平摆放
        legend.setFormSize(0f);
        if(list.size()<20){
            list.add("");
        }
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                try{
                    return list.get((int) value-1);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return null;
            }
        });
        BarData data = new BarData(set);
        aerChart.setTouchEnabled(false);
        aerChart.setDescription(null);
        try{
            aerChart.setData(data);
        }catch (Exception e){
            e.printStackTrace();
        }
        aerChart.invalidate();
    }
    private void initView() {
        aerChart=view.findViewById(R.id.aerChart);
        db = getContext().openOrCreateDatabase("info", getContext().MODE_PRIVATE, null);
    }
}
